package org.jpass.dc.view.upload;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.jpass.dc.view.common.BasicAction;

public class UploadFileAction extends BasicAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 4940347342576144823L;
	/* 上传的文件   */
	private File upload;
	/* 上传的文件名称   */
	private String uploadFileName;
	/* 上传的文件类型   */
	private String uploadContentType;
	/* 保存文件的相对路径  */
	private String savePath;

	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getSavePath() {
		return savePath;
	}
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}


	/*@Override
	public String execute() throws Exception {
		PrintWriter out = this.getResponse().getWriter();
		if (StringUtil.isBlank(savePath)) {
			String msg = "Please select a path to save the upload file.";
			out.write("{success:false, msg: '"+msg+"', type:'warng'");
		} else if (StringUtil.isBlank(uploadFileName)) {
			String msg = "Please select a upload file.";
			out.write("{success:false, msg: '"+msg+"', type:'warng'");
		} else {
			uploadController(); // 开始接收上传文件
		}
		return Action.SUCCESS;
	}*/
	
	/**
	 * 上传文件
	 * @param src
	 * @param dst
	 */
	private  void copy(File src, File dst)  {
        try  {
           InputStream in = null ;
           OutputStream out = null ;
            try  {                
               in = new BufferedInputStream( new FileInputStream(src) );
               out = new BufferedOutputStream( new FileOutputStream(dst));
               byte [] buffer = new byte [in.available()];
               while (in.read(buffer) > 0 )  {
                  out.write(buffer);
              }                
            } finally  {
                if ( null != in)  {
                   in.close();
               } 
                 if ( null != out)  {
                   out.close();
               } 
           } 
        } catch (Exception e)  {
        	
           e.printStackTrace();
       } 
   }
	
	/**
	 * 上传控制文件
	 * @return
	 */
	public String upload() {
		try {
			UploadService.upload(this.getRequest(),this.getResponse()); 
        } catch (IOException e) {  
            LOG.error("上传文件发生异常,错误原因 : " + e.getMessage());  
        }  
        return null; 
	}
	
	/**
	 * 上传进程控制
	 * @return
	 * @throws Exception 
	 */
	public String process() throws Exception{  
        // 缓存progress对象  
        UploadStatus status = (UploadStatus)this.getRequest().getSession().getAttribute("status");  
        System.out.println(status);
        this.getResponse().setContentType("text/html;charset=UTF-8");  
        this.getResponse().setHeader("pragma", "no-cache");  
        this.getResponse().setHeader("cache-control", "no-cache");  
        this.getResponse().setHeader("expires", "0");  
        /*try {  
            //UploadFile.execClientScript(this.getResponse(), callback1 + "(" + key + ")");  
            long temp = 0L;  
            while (!p.isComplete()) {  
                if (temp != p.getCurrentLength()) {  
                    temp = p.getCurrentLength();  
                    // 向客户端显示进度
                    double percentage = p.getUploadPercentage();
                    String msg = "Uploading...";
                    UploadStatus.returnMsg(this.getResponse(), 
                    		"{success:true, msg: '"+msg+"', percentage:'" + percentage + "', finished: false}"); 
                } else {  
                    //LOG.info("progress的状态 ：" + p.isComplete());  
                    //LOG.info("progress上传的数据量 ：+ " + p.getCurrentLength());  
                    //上传进度没有变化时候,不向客户端写数据,写数据过于频繁会让chrome没响应  
                    Thread.sleep(300);  
                }  
                  
            }  
              
        } catch (Exception e) {  
            LOG.error("调用客户端脚本错误,原因 ：" + e.getMessage());  
            p.setComplete(true);  
              
        } */ 
          
        //this.getRequest().getSession().removeAttribute(key);  
        LOG.info("删除progress对象的session key");  
          
        return null;  
	}
}
